/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2010,2011,2012,2013 TELEMATICS LAB, Politecnico di Bari
 *
 * This file is part of LTE-Sim
 *
 * LTE-Sim is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation;
 *
 * LTE-Sim is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LTE-Sim; if not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Giuseppe Piro <g.piro@poliba.it>
 */


#ifndef TRACEBASED_H_
#define TRACEBASED_H_

#include "Application.h"

#include <cstdio>
#include <iostream>
#include <fstream>

/*
 * This application sends udp packets based on a trace file could be downloaded form :
 * http://www.tkn.tu-berlin.de/research/trace/ltvt.html
 * A valid trace file is a file with 4 columns:
 * - the first one represents the frame index
 * - the second one indicates the type of the frame: I, P or B
 * - the third one indicates the time on which the frame was generated by the encoder
 * - the fourth one indicates the frame size in byte
 * if no valid trace trace file is provided to the application the trace from
 * default-trace.h will be loaded.
 */

class TraceBased : public Application {
public:
	TraceBased();
	TraceBased(NetworkNode *source,
			   NetworkNode *destination,
			   int sourcePort,
			   int destinationPort,
			   TransportProtocol::TransportProtocolType protocol);

	virtual ~TraceBased();

    void SetTraceFile(std::string traceFile);
    void LoadTrace (std::string traceFile);
    void LoadDefaultTrace (void);

	virtual void DoStart (void);
	virtual void DoStop (void);

    void ScheduleTransmit (double time);
    void Send (void);

    void UpdateFrameCounter (void);
    int GetFrameCounter (void);

    void PrintTrace (void);

private:

    uint32_t m_TraceSize; //in packet
    double m_interval;
    uint32_t m_size;
    uint32_t m_sent;

    typedef struct
    {
      uint32_t TimeToSend;
      uint16_t PacketSize;
      uint32_t FrameIndex;
      char FrameType;
    } TraceEntry;

    std::vector<TraceEntry> * m_entries;
    std::vector<TraceEntry>::iterator iter;

    int m_frameCounter;
};

#endif /* TRACEBASED_H_ */
